<?php
/*
 * Template Name: product_management
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//Trang này là trang quản lý sản phẩm
//Hiện sản phẩm của USER đó
//User có quyền edit và sửa
//Có thanh search theo tên.
//Fitller theo ngày tháng năm, danh mục.
//Có phân trang.

require_once( dirname( __FILE__ ) . '/controller/product.php' );

$current_user = wp_get_current_user(); // grabs the user info and puts into vars
$display_user_name = cp_get_user_name();
$page = empty($_GET['page']) ? 1 : $_GET['page'];
$amount = empty($_GET['amount']) ? 20 : $_GET['amount'];
$arrData = get_data_product($page, $amount);

?>
<div class="content user-dashboard">

	<div class="content_botbg">

		<div class="content_res">

				<div class="shadowblock_out">

					<div class="shadowblock">

						<h1 class="single dotted">Quản Lý Sản Phẩm</h1>

						<?php do_action( 'appthemes_notices' ); ?>

						<?php
							// setup the pagination and query
							$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
							query_posts( array( 'posts_per_page' => 10, 'post_type' => APP_POST_TYPE, 'post_status' => 'publish, pending, draft', 'author' => $current_user->ID, 'paged' => $paged ) );

							// build the row counter depending on what page we're on
							if ( $paged == 1 ) $i = 0; else $i = $paged * 10 - 10;
						?>

						<?php if ( have_posts() ) : ?>

						<p><?php _e( 'Below you will find a listing of all your classified ads. Click on one of the options to perform a specific task. If you have any questions, please contact the site administrator.', APP_TD ); ?></p>

						<table border="0" cellpadding="4" cellspacing="1" class="tblwide footable">
							<thead>
								<tr>
									<th width="5px" data-class="expand">&nbsp;</th>
									<th class="text-left">&nbsp;<?php _e( 'Title', APP_TD ); ?></th>
									<th width="40px" data-hide="phone"><?php _e( 'Views', APP_TD ); ?></th>
									<th width="80px" data-hide="phone"><?php _e( 'Status', APP_TD ); ?></th>
									<th width="90px" data-hide="phone"><div style="text-align: center;"><?php _e( 'Options', APP_TD ); ?></div></th>
								</tr>
							</thead>
                                                        <tbody>
                                                            <?php
                                                                $i = 0;
                                                                foreach ($arrData["data"] as $value) {
                                                                    $i += 1;
                                                                ?>

								<tr class="even">
									<td class="text-right"><?php echo $i ?></td>

									<td>
										<h3>
                                                                                    <a href=<?php echo get_product_link($value->id)?>><?php echo $value->name ?></a>
                                                                                </h3>

										<div class="meta"><span class="folder"><a rel="tag" href="http://yourstyle.name/?ad_cat=misc"><?php echo get_category($value->category_id)->name ?></a></span> | <span class="clock"><span><?php echo appthemes_display_date( $value->time_created, 'date' ); ?></span></span></div>
                                                                                
									</td>

									<td class="text-center"><?php echo $value->total_view; ?></td>

									<td class="text-center"><span style="color:#33CC33;"><?php echo $value->status; ?></span></td>

									<td class="text-center">
                                                                            <?php
                                                                                $edit_url = get_edit_product_link($value->id);
                                                                                $edit_img = html( 'img', array( 'src' => get_template_directory_uri() . '/images/pencil.png', 'border' => '0', 'title' => __( 'Edit Product', APP_TD ), 'alt' => __( 'Edit Product', APP_TD ) ) );
                                                                                echo html( 'a', array( 'href' => $edit_url, 'title' => __( 'Edit Product', APP_TD ) ), $edit_img ) . ' ';
                                                                                $delete_url = "javascript:void(0);";
                                                                                $delete_img = html( 'img', array( 'src' => get_template_directory_uri() . '/images/cross.png', 'border' => '0', 'title' => __( 'Delete Product', APP_TD ), 'alt' => __( 'Delete Product', APP_TD ) ) );
                                                                                echo html( 'a', array( 'href' => $delete_url,
                                                                                    'title' => __( 'Delete Product', APP_TD ),
                                                                                    'class' => 'deletePost',
                                                                                    'deleteid' => $value->id)
                                                                                        , $delete_img ) . ' ';
                                                                            ?>
									</td>
								</tr>
                                                                <?php } ?>
							</tbody>
						</table>

						<?php if(function_exists('appthemes_pagination')) appthemes_pagination(); ?>

						<?php else : ?>

							<div class="pad10"></div>
							<p class="text-center"><?php _e( 'You currently have no classified ads.', APP_TD ); ?></p>
							<div class="pad10"></div>

						<?php endif; ?>

						<?php wp_reset_query(); ?>

					</div><!-- /shadowblock -->

				</div><!-- /shadowblock_out -->

			<div class="clr"></div>

		</div><!-- /content_res -->

	</div><!-- /content_botbg -->

</div><!-- /content -->

    <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/includes/bootstrap/js/bootstrap.js"></script>
    <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/includes/js/bootbox.js"></script>
<script>
    jQuery(document).ready(function($) {
        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
        $(".deletePost").click(function(){
            var deleteid = $(this).attr("deleteid");
            var deletedRow = $(this).parent().parent();
            bootbox.dialog("Bạn có muốn xóa bài viết này?",
                [ {
                    "label" : "Đồng ý",
                    "class" : "btn-danger",
                    "callback": function() {
                        jQuery.post(
                            ajaxurl,
                            {
                                'action': 'delete_product',
                                'data':   deleteid
                            },
                            function(response){
                                if(response != 0)
                                {
                                    $(deletedRow).fadeOut(1000);
                                    $(deletedRow).animate({
                                        "opacity" : "0",
                                        },{
                                        "complete" : function() {
                                              $(deletedRow).remove();
                                        }
                                    });
                                }
                                else
                                {
                                    alert("có lỗi khi delete!")
                                }
                            }
                        );
                    }
                }, {
                    "label" : "Không xóa",
                    "class" : "btn"
                    //"callback": function() {}
                }]

                ); 
        });
    });
</script>

<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/includes/bootstrap/css/bootstrap.css" /> 